<template>
  <div>
    <FormPanel ref="formpanel" :labelWidth="100" :list="formList" :cols="3" :isSubmitBtn="false" />
    <!-- 库位选择弹窗 -->
    <BaseDialog :visible.sync="positionSelectVisible" title="库位选择" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <position :params="positionFetchParam" @close="positionSelectVisible = false" @onConfirm="handlePositionConfirm" />
    </BaseDialog>
    <div class="btnWrap">
      <el-button @click="cancelBtn">关 闭</el-button>
      <el-button type="primary" @click="saveBtn">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { Storage, queryUsableLocations } from '@/api/part/partOutbound/stockremoval/index';
import { jsonToHump } from '@/utils';
import position from '@/pages/part/common/position';
import { queryControlLocations } from '@/api/part/common';
export default {
  name: 'EditMove',
  components: {
    position
  },
  mixins: [dictionary],
  props: ['editData', 'rowsData'],
  data() {
    this.queryStorage();
    return {
      formList: this.createList(),
      queryData: {},
      partNo: '',
      storageList: [],
      positionFetchParam: {}, // 库位选择查询参数
      positionSelectVisible: false //  库位弹窗是否显示
    };
  },
  mounted() {},
  methods: {
    // 点击选择库位
    handlePositionSelect() {
      let partNo = this.formList.find(item => item.fieldName == 'PART_NO').initialValue;
      let storageCode = this.formList.find(item => item.fieldName == 'NEW_STORAGE_CODE').initialValue;

      this.positionFetchParam = {
        storageCode,
        partNo
      };
      this.positionSelectVisible = true;
    },
    // 库位选择回调
    handlePositionConfirm(val) {
      this.formList.find(item => item.fieldName == 'NEW_STORAGEPOSITION_CODE').initialValue = val[0].storagePositionCode;
      this.positionSelectVisible = false;
    },
    // 仓库选择后回调
    async selectChange(val) {
      // console.log('仓库', val);
      this.positionFetchParam.storageCode = val;
      this.positionFetchParam.partNo = this.formList.find(item => item.fieldName == 'PART_NO').initialValue;
      let positionRes = await queryControlLocations(this.positionFetchParam);
      if (positionRes.resultCode == 200) {
        if (positionRes.data.rows.length && positionRes.data.rows[0].storagePositionCode) {
          this.formList.find(item => item.fieldName === 'NEW_STORAGEPOSITION_CODE').initialValue = positionRes.data.rows[0].storagePositionCode;
        } else {
          this.formList.find(item => item.fieldName === 'NEW_STORAGEPOSITION_CODE').initialValue = null;
        }
      }
      // this.formList.find(item => item.fieldName == 'NEW_STORAGEPOSITION_CODE').initialValue = undefined;
      // if (val == null || val == '' || val == undefined) {
      //   this.formList.find(item => item.fieldName == 'NEW_STORAGEPOSITION_CODE').itemList = [];
      //   this.formList = [...this.formList];
      //   return;
      // }
      // const res = await queryUsableLocations({ storageCode: val, partNo: this.partNo });
      // this.formList.find(item => item.fieldName == 'NEW_STORAGEPOSITION_CODE').itemList = res.data.map(x => ({ text: x.storagePositionCode, value: x.storagePositionCode }));
    },
    createList() {
      return [
        {
          type: 'INPUT',
          label: '原仓库',
          fieldName: 'OLD_STORAGE_NAME',
          // rules: { required: true, message: '请选择原仓库', trigger: 'change' },
          readOnly: true,
          disabled: true
        },
        {
          type: 'INPUT',
          label: '原库位',
          fieldName: 'OLD_STORAGEPOSITION_CODE',
          // rules: { required: true, message: '请选择原仓库', trigger: 'change' },
          readOnly: true,
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'PART_NO',
          readOnly: true,
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'PART_NAME',
          readOnly: true,
          disabled: true
        },
        {
          type: 'SELECT',
          label: '新仓库',
          fieldName: 'NEW_STORAGE_CODE',
          rules: { required: true, message: '请选择新仓库', trigger: 'change' },
          itemList: [],
          change: this.selectChange
        },
        {
          type: 'INPUT_NUMBER',
          label: '移库数量',
          precision: 2,
          fieldName: 'TRANSFER_QUANTITY',
          rules: { required: true, message: '请输入移库数量', trigger: 'change' },
          change: this.changeCostAmount
        },
        {
          type: 'INPUT',
          label: '新库位',
          readOnly: true,
          fieldName: 'NEW_STORAGEPOSITION_CODE',
          rules: { required: true, message: '请选择移库新库位', trigger: 'change' },
          // itemList: [],
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={this.handlePositionSelect}></el-button>;
          }
        },
        {
          type: 'INPUT_NUMBER',
          label: '含税成本单价',
          precision: 2,
          fieldName: 'COST_PRICE',
          readOnly: true,
          hidden: true,
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '含税成本金额',
          precision: 2,
          fieldName: 'COST_AMOUNT',
          readOnly: true,
          disabled: true
        }
      ];
    },
    async queryStorage() {
      try {
        this.partNo = this.editData.PART_NO;
        const res = await queryUsableLocations({ storageCode: this.editData.NEW_STORAGE_CODE, partNo: this.editData.PART_NO });
        const storageList = await Storage();
        // if (res.data.find(item => item.error)) {
        //   this.$emit('delRows', this.editData);
        // } else {
        let temp = this.formList.find(item => item.fieldName === 'NEW_STORAGEPOSITION_CODE');
        temp.itemList = this.editData.data.map(x => ({ text: x.storagePositionCode, value: x.storagePositionCode }));
        this.formList.find(item => item.fieldName === 'OLD_STORAGE_NAME').initialValue = this.editData.OLD_STORAGE_NAME;
        this.formList.find(item => item.fieldName === 'PART_NO').initialValue = this.editData.PART_NO;
        this.formList.find(item => item.fieldName === 'PART_NAME').initialValue = this.editData.PART_NAME;
        this.formList.find(item => item.fieldName === 'TRANSFER_QUANTITY').initialValue = this.editData.TRANSFER_QUANTITY;
        this.formList.find(item => item.fieldName === 'OLD_STORAGEPOSITION_CODE').initialValue = this.editData.OLD_STORAGEPOSITION_CODE;
        this.formList.find(item => item.fieldName === 'COST_PRICE').initialValue = this.editData.COST_PRICE;
        this.formList.find(item => item.fieldName === 'COST_AMOUNT').initialValue = this.editData.COST_AMOUNT;

        this.storageList = this.formList.find(item => item.fieldName === 'NEW_STORAGE_CODE').itemList = storageList.data.map(x => ({
          text: x.STORAGE_NAME,
          value: x.STORAGE_CODE,
          OEM_TAG: x.OEM_TAG,
          STORAGE_TYPE: x.STORAGE_TYPE
        }));
        this.formList.find(item => item.fieldName === 'NEW_STORAGE_CODE').initialValue = this.editData.NEW_STORAGE_CODE;
        this.formList.find(item => item.fieldName === 'NEW_STORAGEPOSITION_CODE').initialValue = this.editData.NEW_STORAGEPOSITION_CODE;
        // }
      } catch (error) {}
    },
    saveBtn() {
      try {
        let formData = this.$refs.formpanel.SUBMIT_FORM();
        let value = {
          OLD_STORAGE_CODE: this.editData.OLD_STORAGE_CODE,
          NEW_STORAGE_CODE: formData.NEW_STORAGE_CODE,
          NEW_STORAGE_NAME: this.storageList.find(x => x.value == formData.NEW_STORAGE_CODE).text,
          PART_NO: formData.PART_NO,
          PART_NAME: formData.PART_NAME,
          TRANSFER_QUANTITY: formData.TRANSFER_QUANTITY,
          NEW_STORAGEPOSITION_CODE: formData.NEW_STORAGEPOSITION_CODE,
          index: this.editData.index,
          uid: this.editData._uid
        };
        console.log(value, this.editData, 'value');
        if (value.OLD_STORAGE_CODE == value.NEW_STORAGE_CODE) {
          return this.$notify.warning({ title: '错误提示', message: '新旧仓库不能一样！' });
        }
        // console.log(this.storageList.find(x => x.value == formData.NEW_STORAGE_CODE).OEM_TAG, this.storageList.find(x => x.value == this.editData.OLD_STORAGE_CODE).OEM_TAG, 'OEM_TAG');
        if (this.storageList.find(x => x.value == formData.NEW_STORAGE_CODE).OEM_TAG != this.storageList.find(x => x.value == this.editData.OLD_STORAGE_CODE).OEM_TAG) {
          return this.$notify.warning({ title: '错误提示', message: '原厂库与非原厂库之间不能移库！' });
        }
        console.log(this.storageList.find(x => x.value == formData.NEW_STORAGE_CODE).STORAGE_TYPE, 'new', this.storageList.find(x => x.value == this.editData.OLD_STORAGE_CODE).STORAGE_TYPE, 'old');
        console.log(this.storageList, 'this.storageList');
        // if (this.storageList.find(x => x.value == formData.NEW_STORAGE_CODE).STORAGE_TYPE != this.storageList.find(x => x.value == this.editData.OLD_STORAGE_CODE).STORAGE_TYPE) {
        //   return this.$notify.warning({ title: '错误提示', message: '油车件库与电车件库之间不能移库！' });
        // }
        // jsonToHump(value);
        // console.log(value);
        let tempVal = {};
        if (this.rowData) {
          tempVal = this.rowData.find(item => item.NEW_STORAGE_CODE == value.NEW_STORAGE_CODE);
          // console.log('parent', this.rowData, tempData);
        }

        // console.log('重复数据', tempVal);
        if (tempVal) {
          if (tempVal.NEW_STORAGEPOSITION_CODE == value.NEW_STORAGEPOSITION_CODE) {
            return this.$notify.warning({ title: '错误提示', message: '该库位已被占用' });
          }
        }
        this.$emit('closeFour', value);
      } catch (error) {}
    },
    cancelBtn() {
      this.$emit('closeFour');
    },
    changeCostAmount(val) {
      this.formList.find(item => item.fieldName === 'COST_AMOUNT').initialValue = val * this.formList.find(item => item.fieldName === 'COST_PRICE').initialValue;
    }
  }
};
</script>

<style scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
